<template>
  <div class="product-detail">
    <nav-bar class="header">
      <div slot="center">商品详情</div>
    </nav-bar>
    <div class="detail-content">
      <div class="detail-swipe-wrap">
        <van-swipe class="my-swipe" indicator-color="#ff8198">
          <van-swipe-item v-for="(item,index) in detail.goodsCarouselList" :key="index">
            <img :src="prefix(item)" alt="">
          </van-swipe-item>
        </van-swipe>
      </div>
      <div class="product-info">
        <div class="product-title">
          {{detail.goodsName}}
        </div>
        <div class="product-desc">免邮费 顺丰快递</div>
        <div class="product-price">
          <span>￥{{detail.sellingPrice}}</span>
        </div>
      </div>
      <div class="product-intro">
        <ul>
          <li>概述</li>
          <li>参数</li>
          <li>安装服务</li>
          <li>常见问题</li>
        </ul>
        <div class="product-content" v-html="detail.goodsDetailContent"></div>
        <van-goods-action>
          <van-goods-action-icon icon="chat-o" text="客服"/>
          <van-goods-action-icon icon="cart-o" text="购物车" :info="!count?'':count" @click="goCart()"/>
          <van-goods-action-button type="warning" @click="addCart" text="加入购物车" color="rgb(255, 129, 152,.7)"/>
          <van-goods-action-button @click="goToCart" color="#ff8198" text="立即购买" />
        </van-goods-action>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import NavBar from 'components/common/navbar/NavBar'
import { Swipe, SwipeItem, GoodsAction, GoodsActionIcon, GoodsActionButton,} from 'vant'

import { getDetail } from 'network/good'
import { addCart } from 'network/cart'
Vue.use(Swipe).use(SwipeItem).use(GoodsAction).use(GoodsActionIcon).use(GoodsActionButton)
export default {
  data(){
    return {
      detail: {
        goodsCarouselList: []
      }
    }
  },
  components:{
    NavBar,

  },
  computed: {
    count () {
      return this.$store.state.cartCount
    }
  },
  async mounted() {
    const { id } = this.$route.params
    const { data } = await getDetail(id)
    this.detail = data
    console.log(data);

  },
  methods: {
    goCart() {
      this.$router.push({ path: '/cart' })
    },
    async addCart() {
      const { data, resultCode } = await addCart({goodsCount: 1, goodsId: this.detail.goodsId})

      if(resultCode === 200){
        this.$toast.show('添加成功')
        this.$store.dispatch('updateCart')
      }
    },
    async goToCart() {
      const { data, resultCode } = await addCart({ goodsCount: 1, goodsId: this.detail.goodsId })
      this.$store.dispatch('updateCart')
      this.$router.push({ path: '/cart' })
    }
  },
}
</script>
<style lang="less" scoped>
  
  .product-detail {
    
    .detail-content {
      margin-top: 44px;
      .detail-swipe-wrap {
        .my-swipe .van-swipe-item {
          img {
            width: 100%;
            // height: 300px;
          }
        }
      }
      .product-info {
        padding: 0 10px;
        .product-title {
          font-size: 18px;
          text-align: left;
          color: #333;
        }
        .product-desc {
          font-size: 14px;
          text-align: left;
          color: #999;
          padding: 5px 0;
        }
        .product-price {
          span:nth-child(1) {
            color: #F63515;
            font-size: 22px;
          }
          span:nth-child(2) {
            color: #999;
            font-size: 16px;
          }
        }
      }
      .product-intro {
        width: 100%;
        ul {
          width: 100%;
          margin: 10px 0;
          display: flex;
          li {
            flex: 1;
            padding: 5px 0;
            text-align: center;
            font-size: 15px;
            border-right: 1px solid #999;
            box-sizing: border-box;
            &:last-child {
              border-right: none;
            }
          }
        }
        .product-content {
          padding: 0 20px;
          width: 100%;
           /deep/  img {
            width: 100%!important;
          }
        }
      }
    }
    .van-goods-action-button--warning {
      background: linear-gradient(to right,#6bd8d8, #fff)
    }
    .van-goods-action-button--danger {
      background: linear-gradient(to right, #0dc3c3, #098888)
    }
  }
</style>
